<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="true" title="上传照片" back-icon-color='#333333' height="50" :border-bottom="false">
		</u-navbar>
		<u-select v-model="show" :list="typeLists" @confirm="confirm"></u-select>
		<view class="list">
			<view class="title">
				<text>{{title}}</text>
				<u-input v-model="content" style="margin-left:200rpx; width: 200rpx;" :type="type" placeholder="单行输入"
					:border="border" input-align="right" />
			</view>
			<view class="sort">
				<text>{{select_sort}}</text>
				<text>{{study}}</text>
				<text @click="show=true"></text>
			</view>
			<view class="photo">
				<view class="per_photo" v-for="(item,index) in  photo_list" :key="index">
					<image :src="item" mode="widthFix"></image>
				</view>
				<image src="../../static/tupian107.png" mode="widthFix" id="img" @click.stop="addimg"></image>
			</view>
			<view class="blank" style="width: 94%; margin-left: 3%; height: 100rpx;">

			</view>
		</view>
		<view class="confirm">
			<view class="verify" @click="publishGallery">
				{{verify}}
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				show: false,
				title: '标题',
				select_sort: '选择分类',
				study: '',
				content: '',
				type_id: '',
				type: 'text',
				verify: '确认',
				border: false,
				type_list: [],
				photo_list: [],
				form: {
					gallery_type_id: "",
					main_title: "",
					picture_json: "",
				}

			}
		},
		onLoad() {
			this.getGalleryTypeList();
		},
		computed: {
			typeLists() {
				if (this.type_list) {
					let arr = [];
					this.type_list.forEach(item => {
						arr.push({
							label: item.name,
							value: item.gallery_type_id
						});
					})
					return arr;
				} else {
					return [];
				}
			}
		},
		methods: {
			addimg() {
				let _this = this;
				let base_url = uni.getStorageSync('host');
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						const uploadTask = uni.uploadFile({
							formData: {
								image: res.tempFilePaths[0],
							},
							url: base_url + 'system/image/upload',
							filePath: res.tempFilePaths[0],
							name: 'image',
							success: function(res) {
								let data = JSON.parse(res.data)
								console.log(data.data.url);
								_this.photo_list.push(data.data.url);
							}
						});
					}
				});
			},


			confirm(e) {
				console.log(e);
				this.study = e[0].label;
				this.type_id = e[0].value;
			},
			publishGallery() {
				let arr = [];
				if (this.content) {
					let base_url = uni.getStorageSync('host');
					console.log(base_url);
					if (this.photo_list) {
						for (let i = 0; i < this.photo_list.length; i++) {
							arr.push({
								img: this.photo_list[i],
								desc: ''
							})
						}
					}
					let _this = this;
					request({
						url: base_url + 'teacher/gallery/publish',
						method: 'post',
						data: {
							gallery_type_id: this.type_id,
							main_title: this.content,
							picture_json: JSON.stringify(arr),
						},
						success(res) {
							if (res.statusCode == 200) {
								_this.$toLink('../campus_album/campus_album', {})
							} else {
								_this.$refs.uToast.show({
									title: res.data.message || '系统错误',
									type: 'warning',
								})
							}

						}
					})
				} else {
					this.$refs.uToast.show({
						title: '请输入标题!',
						type: 'warning',
					})
				}
			},
			getGalleryTypeList() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/gallery/galleryTypeList',
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							_this.type_list = res.data.data;
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F0F0F0;
		overflow: hidden;

		.list {
			width: 710rpx;
			margin-top: 20rpx;
			margin-left: 20rpx;
			background-color: #fff;
			border-radius: 12rpx;

			.title {
				width: 94%;
				margin-left: 3%;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 84rpx;
				border-bottom: 1px solid #F2F2F2;

				text {
					&:nth-child(1) {
						color: #999999;
						font-size: 26rpx;
					}
				}

			}

			.sort {
				width: 94%;
				margin-left: 3%;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 84rpx;
				border-bottom: 1px solid #F2F2F2;

				text {
					color: #999999;
					font-size: 26rpx;

					&:nth-child(1) {}

					&:nth-child(2) {
						color: #333333;
						width: 480rpx;
						text-align: right;
					}

					&:nth-child(3) {
						width: 0rpx;
						margin-left: 20rpx;
						margin-top: 20rpx;
						height: 0rpx;
						border-width: 18rpx 18rpx 18rpx 18rpx;
						border-color: #ccc transparent transparent transparent;
						border-style: solid;
					}
				}

			}

			.photo {
				width: 94%;
				margin-left: 3%;
				margin-top: 47rpx;
				display: flex;
				justify-content: flex-start;
				flex-wrap: wrap;

				.per_photo {
					width: 155rpx;
					height: 155rpx;
					overflow: hidden;
					margin-left: 25rpx;

					image {
						width: 155rpx;
						height: 155rpx;
					}
				}

				#img {
					width: 155rpx;
					margin-left: 25rpx;
				}
			}

		}

		.confirm {
			width: 100%;
			// margin-top: 400rpx;
			height: 300rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.verify {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: linear-gradient(to right, #2C78FE, #32B0FF);
				color: #fff;
				font-size: 30rpx;
			}
		}



	}
</style>
